package com.calvin.study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.calvin.study.mapper.StorageMapper;

import cloud.alibaba.study.dto.storage.StorageDto;
import cloud.alibaba.study.entity.base.ResponseVo;
import cloud.alibaba.study.entity.storage.Storage;

@Service
public class StorageService {

	@Autowired
	private StorageMapper storageMapper;

	@Transactional
	public ResponseVo storageReserved(Storage input) throws Exception {

		QueryWrapper<Storage> queryWrapper = new QueryWrapper<Storage>();
		queryWrapper.eq("product_id", input.getProductId());
		Storage storage = storageMapper.selectOne(queryWrapper);

		if (null == storage) {
			throw new Exception("该商品暂无库存");
		}

		if (storage.getResidue() <= 0) {
			throw new Exception("该商品库存已售罄");
		}

		//依据订单类型处理业务
		//如果是普通订单，则需要先预占库存，从重新计算库存量
		//如果是秒杀订单，在活动上架时，已预占库存，无需处理
		Storage update = new Storage();
		update.setReserved(storage.getReserved() == 0 ? input.getReserved() : storage.getReserved() + input.getReserved());
		update.setResidue(storage.getResidue() - input.getReserved());

		QueryWrapper<Storage> qw = new QueryWrapper<Storage>();
		qw.eq("product_id", input.getProductId());

		int execFlag = storageMapper.update(update, qw);
		if (execFlag == 0) {
			throw new Exception("商品[" + storage.getProductId() + "]，库存预占失败");
		}
		return ResponseVo.success("商品[" + storage.getProductId() + "]，库存预占成功");
	}

	
	/**
	 * 解除库存预占
	 */
	@Transactional
	public ResponseVo storageUnReserved(StorageDto input) throws Exception {
		
		//查询订单信息
		input.getOrderNo();
		

		QueryWrapper<Storage> queryWrapper = new QueryWrapper<Storage>();
		queryWrapper.eq("product_id", input.getProductId());
		Storage storage = storageMapper.selectOne(queryWrapper);

		if (null == storage) {
			throw new Exception("该商品暂无库存");
		}

		if (storage.getResidue() <= 0) {
			throw new Exception("该商品库存已售罄");
		}

		Storage update = new Storage();
		update.setReserved(0);
		update.setResidue(storage.getResidue() + storage.getReserved());
		
		QueryWrapper<Storage> qw = new QueryWrapper<Storage>();
		qw.eq("product_id", input.getProductId());

		int execFlag = storageMapper.update(update, qw);
		if (execFlag == 0) {
			throw new Exception("商品[" + storage.getProductId() + "]，解除库存预占失败");
		}
		return ResponseVo.success("商品[" + storage.getProductId() + "]，解除库存预占成功");
	}

	//付款成功，减扣库存
	@Transactional
	public ResponseVo reduceInventory(Storage input) throws Exception {

		QueryWrapper<Storage> queryWrapper = new QueryWrapper<Storage>();
		queryWrapper.eq("product_id", input.getProductId());
		Storage storage = storageMapper.selectOne(queryWrapper);

		if (null == storage) {
			throw new Exception("该商品暂无库存");
		}

		if (storage.getResidue() <= 0) {
			throw new Exception("该商品库存已售罄");
		}
		//普通订单
		Storage update = new Storage();
		update.setReserved(0);
		update.setUsed(storage.getReserved());

		QueryWrapper<Storage> qw = new QueryWrapper<Storage>();
		qw.eq("product_id", input.getProductId());
		int execFlag = storageMapper.update(update, qw);

		if (execFlag == 0) {
			throw new Exception("扣减库存失败");
		}
		return ResponseVo.success("商品[" + storage.getProductId() + "]，库存扣减成功");
	}

}
